package com.wanmait.gohome.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.gohome.mapper.AdminMapper;
import com.wanmait.gohome.pojo.Admin;
import com.wanmait.gohome.service.AdminService;
import com.wanmait.gohome.util.SysContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AdminServiceImpl implements AdminService {
    @Resource
    private AdminMapper adminMapper;
//登陆
    @Override
    public Admin findByAccountAndPass(String account,String password,HttpServletRequest request) {
        Admin loginAdmin  = adminMapper.findByAccountAndPass(account,password);
         if(loginAdmin!=null){
             Admin newAdmin = new Admin();
             newAdmin.setLoginNum(loginAdmin.getLoginNum()+1);
             newAdmin.setLoginTime(new Date());
             String LoginIp = request.getRemoteAddr();
             newAdmin.setLoginIP(LoginIp);
             newAdmin.setId(loginAdmin.getId());
             adminMapper.update(newAdmin);
        }
        return loginAdmin;
    }

    @Override
    public Admin findInfo(String account) {
        return adminMapper.findByAccount(account);
    }

    //检查输入的旧密码是否一样
    @Override
    public Admin findOldPass(String password, Integer id) {
        return adminMapper.findOldPass(password,id);
    }
//修改密码
    @Override
    public void updatePass(String password, Integer id) {
         adminMapper.updatePass(password,id);
    }

    @Override
    public int findMark(String account) {
        return adminMapper.fingByMark(account);
    }
//封禁
    @Override
    public void banned(Integer id) {
   adminMapper.banned(id);
    }
    //解封
    @Override
    public void deblocking(Integer id) {
    adminMapper.deblocking(id);
    }

    @Override
    public void cleanLoginIp(Integer id) {
      adminMapper.cleanLoginInp(id);
    }

    @Override
    public void insertReject(Admin admin) {
        adminMapper.insertReject(admin);
    }


    //添加管理员
    @Override
    public void insert(Admin admin) {
        adminMapper.insert(admin);
    }

    @Override
    public void update(Integer id) {
        adminMapper.updateAdmin(id);
    }

    @Override
    public void delete(Integer id) {

    }

    @Override
    public void delete(String ids) {

    }

    @Override
    public List<Admin> findAll() {
        return null;
    }

    @Override
    public Admin findById(Integer id) {
        return null;
    }


    @Override
    public List<Admin> findAll(Admin object) {
        return null;
    }

    @Override
    public PageInfo<Admin> findPage(Admin Object) {
        return null;
    }

    @Override
    public PageInfo<Admin> findPage() {
        int pageNum = 1;
        int pageSize = 10;
        PageHelper.startPage(pageNum,pageSize);
        List<Admin> admins = adminMapper.selectList();
        PageInfo<Admin> pageInfo = new PageInfo<>(admins,5);
        return pageInfo;
    }
}
